Phone Number Validation Endpoint
This document provides comprehensive documentation for the /validate-number endpoint, which validates and cleans phone numbers for the WhatsApp bulk messaging system. The endpoint accepts a POST request with a JSON payload containing a phone number string, applies standardized cleaning rules, and returns a normalized result indicating validity and the cleaned number format.
The validation algorithm focuses on:
Extracting only digits while handling international prefixes and separators
Enforcing length constraints (minimum 7 and maximum 15 digits)
Normalizing international formats and country codes
Returning a structured response with validation status, cleaned number, and original input
The phone number validation functionality resides in the Python backend module alongside other contact processing utilities. The Flask application exposes the /validate-number endpoint and shares the same cleaning logic used across the system.
app.py"] B["Validation Logic
validate_number.py"] C["Requirements
requirements.txt"] end subgraph "Documentation" D["Backend README
python-backend/README.md"] E["Project README
README.md"] end A --> B A --> C D --> A E --> A
Diagram sources
Section sources
Flask Application: Provides the
/validate-numberendpoint and shared phone number cleaning logic.Validation Module: Implements the cleaning algorithm used by the endpoint and other contact processing utilities.
Requirements: Defines runtime dependencies for the Flask application and utilities.
Key responsibilities:
Expose
/validate-numberendpoint with POST methodParse JSON request body for the
numberfieldClean and normalize phone numbers using consistent rules
Return standardized response schema with validation outcome
Section sources
The /validate-number endpoint integrates with the broader contact processing pipeline. It shares the same cleaning logic used by file import and manual number parsing utilities, ensuring consistent normalization across the application.
Diagram sources
Endpoint Definition and Request Schema#
Method: POST
Path:
/validate-numberContent-Type: application/json
Request Body:
number: string (required)
Response Schema:
valid: boolean (true if cleaned_number is not null)cleaned_number: string|null (normalized phone number or null if invalid)original: string (original input value)
Behavior:
Validates presence of the
numberfieldApplies cleaning and normalization rules
Returns standardized result regardless of input format
Section sources
Validation Algorithm Details#
The cleaning algorithm follows a deterministic sequence to normalize phone numbers:
Diagram sources
Step-by-Step Processing#
Input Validation: Reject empty or null inputs immediately
Whitespace Normalization: Strip leading/trailing spaces
Separator Removal: Eliminate common phone number separators
Character Filtering: Retain only digits and the plus sign
International Prefix Handling:
Remove leading zeros when not international
Prepend plus sign when length > 10 and no plus
Length Validation: Enforce 7-15 digit constraint
Output: Return normalized number or null if invalid
Section sources
Response Schema Specification#
The endpoint consistently returns a JSON object with three fields:
valid: boolean indicating whether the number passed validationcleaned_number: string containing the normalized phone number or nulloriginal: string containing the exact input received
This schema enables downstream systems to:
Determine immediate usability of the number
Access both original and normalized forms for logging
Integrate seamlessly with contact import workflows
Section sources
Practical Examples#
Valid Inputs and Expected Outcomes#
Input:
"+1-555-123-4567"Output:
{"valid": true, "cleaned_number": "+15551234567", "original": "+1-555-123-4567"}
Input:
"(555) 123-4567"Output:
{"valid": true, "cleaned_number": "+15551234567", "original": "(555) 123-4567"}
Input:
"0015551234567"Output:
{"valid": true, "cleaned_number": "+15551234567", "original": "0015551234567"}
Input:
"5551234567"Output:
{"valid": true, "cleaned_number": "+5551234567", "original": "5551234567"}
Invalid Inputs and Expected Outcomes#
Input:
"123"Output:
{"valid": false, "cleaned_number": null, "original": "123"}
Input:
"1234567890123456"Output:
{"valid": false, "cleaned_number": null, "original": "1234567890123456"}
Input:
"abc-def-ghij"Output:
{"valid": false, "cleaned_number": null, "original": "abc-def-ghij"}
Input:
""Output:
{"valid": false, "cleaned_number": null, "original": ""}
Edge Cases#
Input:
"++15551234567"Output:
{"valid": true, "cleaned_number": "+15551234567", "original": "++15551234567"}
Input:
" +1 555 123 4567 "Output:
{"valid": true, "cleaned_number": "+15551234567", "original": " +1 555 123 4567 "}
Input:
"123-456-7890123456"(exceeds 15 digits)Output:
{"valid": false, "cleaned_number": null, "original": "123-456-7890123456"}
Section sources
Integration Patterns with Contact Import Workflows#
The /validate-number endpoint complements the broader contact processing pipeline:
/parse-manual-numbers"] B["File Upload
/upload"] C["Single Number
/validate-number"] end subgraph "Shared Logic" D["clean_phone_number()"] end subgraph "Downstream Systems" E["WhatsApp Messaging"] F["Email Processing"] G["Contact Storage"] end A --> D B --> D C --> D D --> E D --> F D --> G
Integration benefits:
Consistent normalization across all input methods
Reduced duplicate validation logic
Unified error handling and logging
Seamless fallback behavior when backend is unavailable
Diagram sources
Section sources
The endpoint relies on shared dependencies defined in the requirements file:
Diagram sources
Section sources
Algorithm Complexity: O(n) where n is the length of the input string
Memory Usage: Proportional to input size with minimal intermediate allocations
Regex Operations: Single-pass cleaning with predictable performance characteristics
Scalability: Suitable for batch processing with minimal overhead
Best practices:
Use streaming for large datasets when applicable
Cache frequently processed numbers if needed
Consider batching multiple validations for improved throughput
Common issues and resolutions:
Request Validation Errors#
Missing
numberfield: Returns 400 with error messageMalformed JSON: Flask handles automatically
Non-string values: Converted to string during processing
Validation Failures#
Numbers outside 7-15 digit range: Consider as invalid
Unparsable formats: Return null cleaned_number
Empty or whitespace-only inputs: Return null cleaned_number
Integration Issues#
CORS problems: Flask-CORS is enabled globally
Port conflicts: Default port 5000 can be configured
Dependency issues: Ensure requirements are installed
Section sources
The /validate-number endpoint provides a robust, standardized mechanism for phone number validation and cleaning within the WhatsApp bulk messaging system. Its consistent algorithm ensures reliable normalization across diverse input formats, while the unified response schema facilitates seamless integration with contact import workflows and downstream messaging systems. The implementation balances simplicity with comprehensive coverage of international phone number formats, making it suitable for production deployment in multi-country messaging scenarios.